package config;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import main.WoTCWBot;

import org.ini4j.Wini;

import web.interfaces.TankSicherheitModul;

public class Constants {
	/*
	 * ts3
	 */
	public static String TS3_IP = "127.0.0.1";
	public static int TS3_PORT = 9987;
	public static int TS3_QUERY_PORT = 10011;

	public static String TS3_QUERY_USERNAME="WoTCWBot";
	public static String TS3_QUERY_PASSWORD = "password";
	public static String TS3_DISPLAY_NAME = "WoTCWBot";
	
	public static int TS3_SERVER_ID=1;
	/*
	 * general
	 */
	public static int CLAN_ID = 500011653; //500011653 ICE
	public static String CLAN_NAME = null;
	public static int TIMEZONE_OFFSET = 1;
	
	/*
	 * debug
	 */
	public static boolean SEND_ALWAYS = false;
	
	
	/*
	 *  intern
	 */
	public static URL URL_BATTLE_SCHEDULE=null;
	public static List<URL> URLS_REGIONS = new ArrayList<URL>();
	
	public static String MODULE_HOMEPATH = TankSicherheitModul.TANKSICHERHEIT_HOMEPATH + "WoTCWBot" + File.separator;

	public static boolean loadIni()
	{
		try
		{
			File fini = new File(MODULE_HOMEPATH+"WoTCWBot.ini");
			System.out.println(fini.getAbsolutePath());
			WoTCWBot.LOGGER.debug("loading "+fini.getAbsolutePath());
			if (fini.exists()) 
			{
				Wini ini = new Wini(fini);
				//TS3
				TS3_IP = ini.get("TS3CONFIG", "ts3ip",String.class);
				TS3_PORT = ini.get("TS3CONFIG","port",int.class);
				TS3_QUERY_PORT = ini.get("TS3CONFIG", "ts3queryport",int.class);
				TS3_QUERY_USERNAME = ini.get("TS3CONFIG", "username",String.class);
				TS3_QUERY_PASSWORD = ini.get("TS3CONFIG", "password",String.class);
				TS3_DISPLAY_NAME = ini.get("TS3CONFIG", "displayname",String.class);
				
				//GENERAL
				CLAN_ID = ini.get("WOTCONFIG","clanid",int.class);
				TIMEZONE_OFFSET = ini.get("WOTCONFIG","timezoneOffset",int.class);
				
				//DEBUG
				SEND_ALWAYS = ini.get("DEBUG","sendAlways",boolean.class);
			} 
			else 
			{
				/*
				 * creating WoTCWBot.ini with default values
				 */

				WoTCWBot.LOGGER.debug("WoTCWBot.ini not found, but created with default values.");
				Files.createDirectories(Paths.get(MODULE_HOMEPATH));
				Files.createFile(fini.toPath());
				Wini ini = new Wini(fini);
				
				//TS3
				ini.put("TS3CONFIG", "ts3ip",TS3_IP);
				ini.put("TS3CONFIG", "port",TS3_PORT);
				ini.put("TS3CONFIG", "ts3queryport",TS3_QUERY_PORT);
				ini.put("TS3CONFIG", "username",TS3_QUERY_USERNAME);
				ini.put("TS3CONFIG", "password",TS3_QUERY_PASSWORD);
				ini.put("TS3CONFIG", "displayname",TS3_DISPLAY_NAME);
				
				
				ini.put("WOTCONFIG","clanid",CLAN_ID);
				ini.put("WOTCONFIG","timezoneOffset",TIMEZONE_OFFSET);
				
				ini.put("DEBUG", "sendAlways",SEND_ALWAYS);
				ini.store();
				return false;
			} //_=1364670472087&
			URL_BATTLE_SCHEDULE = new URL("http://worldoftanks.eu/community/clans/"+Constants.CLAN_ID+"/battles/list/?offset=0&limit=1000&order_by=time&search=&echo=1&id=js-battles-table");
			return true;
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			WoTCWBot.LOGGER.fatal("error loading ini file: "+e.toString());
		}
		return false;
	}
}
